package com.liugh.controller.zhongche;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.base.BusinessException;
import com.liugh.base.Constant;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.User;
import com.liugh.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Fatao on 2018/11/9 .
 */
@RestController
@RequestMapping("/user_manage")
@Api(description="用户管理模块")
public class ZhongCheUserController extends BaseRestController {


    @Autowired
    private IUserService userService;



    /**
     * 添加用户的方法 .
     * @param user .
     * @return .
     */
    @ApiOperation("添加用户")
    @PostMapping("/add_user")
    public ResponseModel<User> addUser(@RequestBody User user) throws BusinessException {
        if (!Validator.isMobile(user.getMobile())) {
            throw new BusinessException(PublicResultConstant.PARAM_ERROR);
        }
        if (userService.getUserByMobile(user.getMobile()) != null) {
            throw new BusinessException(PublicResultConstant.PHONE_NUMBER_EXIST);
        }
        if (userService.addUser(user) == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(user);
        }
    }


    /**
     * 根据问题回答,更改密码 .
     *
     * @param questionType .
     * @param answer       .
     * @param newPassword  .
     * @return .
     */
    @ApiOperation("忘记密码")
    @PutMapping("/forget_password")
    public ResponseModel forgetPassword(@RequestParam("mobile") String mobile, @RequestParam("questionType") int questionType,
                                        @RequestParam("answer") String answer, @RequestParam("newPassword") String newPassword) throws BusinessException {
        User user = userService.getUserByMobile(mobile);
        if (userService.getUserByMobile(mobile) == null) {
            return validationFailure(PublicResultConstant.INVALID_USER);
        } else {
            if (user.getQuestionType() == questionType && user.getAnswer().equals(answer)) {
                if (!userService.forgetPassword(user, newPassword)) {
                    throw new BusinessException(PublicResultConstant.ERROR);
                } else {
                    return fillSuccessData(PublicResultConstant.SUCCEED);
                }
            } else {
                return fillFailData(PublicResultConstant.QUESTION_OR_ANSWER_NOT_RIGHT);
            }

        }

    }

    /**
     * 获取所有的用户 .
     * @param pageNum .
     * @param pageSize .
     * @return
     */
    @ApiOperation("获取所有的用户")
    @GetMapping("/get_all")
    public ResponseModel<Page<User>> getAllUser(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        return fillSuccessDataPage(userService.getAllUser(new Page<>(pageNum, pageSize)));
    }


    /**
     * 根据id获取用户 .
     * @param userNo .
     * @return .
     */
    @ApiOperation("根据userNo获取用户")
    @GetMapping("/get")
    public ResponseModel<User> getUserById(@RequestParam("userNo") String userNo) {
        return fillSuccessData(userService.getUserByUserNo(userNo));
    }

    /**
     * 更新用户信息 .
     *
     * @param user .
     * @return .
     */
    @ApiOperation("更新用户的方法")
    @PutMapping("/update")
    public ResponseModel<User> updateUser(@RequestBody User user) throws BusinessException {
        boolean result = userService.update(user, new EntityWrapper<User>().eq("user_no", user.getUserNo()));
        if (result) {
            return fillSuccessData(user);
        } else {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
    }


    /**
     * 禁用用户 .
     * @param userNo .
     * @return .
     */
    @ApiOperation("禁用一个用户")
    @PutMapping("/disable")
    public ResponseModel disableUserByUserNo(@RequestParam("userNo") String userNo) {
        User user = userService.getUserByUserNo(userNo);
        user.setStatus(Constant.UserStatus.DISABLED);
        boolean isSuccess = userService.update(user, new EntityWrapper<User>().eq("user_no", userNo));
        return isSuccess ? fillSuccessData(PublicResultConstant.SUCCEED) : fillFailData(PublicResultConstant.ERROR);
    }
}
